package fr.ups.jim.isi.tp2;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ColorUIResource;

public class MyColorChooser extends JFrame implements ActionListener, ChangeListener{

	private static final long serialVersionUID = 1L;

	private JSlider js_red, js_green, js_blue;
	private int red_value, green_value, blue_value;
	private String hex_value; 
	private JTextField jtf_red, jtf_green, jtf_blue, jtf_hex;
	private JPanel jp_left, jp_right, jp_color_display;
	private Color color_display;
	
	public MyColorChooser() {
		this.setResizable(false);
		this.setTitle("RGBSelector");
		this.setSize(500, 250);
		
		// create those components
		// left part componets :
		js_red = new JSlider(0, 255, 128);
		js_green = new JSlider(0, 255, 128);
		js_blue = new JSlider(0, 255, 128);
		jtf_red = new JTextField(5);
		jtf_green = new JTextField(5);
		jtf_blue = new JTextField(5);
		jp_left = new JPanel();
		// right part components:
		jtf_hex = new JTextField(10);
		jp_color_display = new JPanel();
		jp_right = new JPanel();
		
		// register Slider's ChangeListener
		js_red.addChangeListener(this);
		js_green.addChangeListener(this);
		js_blue.addChangeListener(this);
		
		// register TextField's ActionListener
		jtf_red.addActionListener(this);
		jtf_green.addActionListener(this);
		jtf_blue.addActionListener(this);
		jtf_hex.addActionListener(this);
			
		// create three layout for the main container, 
		// left part container and right part
		GridBagLayout gbl_main = new GridBagLayout();
		GridBagLayout gbl_left = new GridBagLayout();
		GridBagLayout gbl_right = new GridBagLayout();
		this.setLayout(gbl_main);
		jp_left.setLayout(gbl_left);
		jp_right.setLayout(gbl_right);
		
		// create GridBagLayoutConstraints for left JPanel
		GridBagConstraints gbc_jslider = new GridBagConstraints();
		gbc_jslider.gridwidth = GridBagConstraints.RELATIVE;
		GridBagConstraints gbc_jtext1 = new GridBagConstraints();
		gbc_jslider.ipady = 20;
		gbc_jtext1.gridwidth = GridBagConstraints.REMAINDER;
		
		// place 3 scrollbars and 3 textfield in left JPanel
		jp_left.add(js_red, gbc_jslider);
		jp_left.add(jtf_red, gbc_jtext1);
		
		jp_left.add(js_green, gbc_jslider);
		jp_left.add(jtf_green, gbc_jtext1);
		
		jp_left.add(js_blue, gbc_jslider);
		jp_left.add(jtf_blue, gbc_jtext1);
		
		// create GridBagLayoutConstraints for right JPanel
		GridBagConstraints gbc_jtf_hex = new GridBagConstraints();
		gbc_jtf_hex.gridwidth = GridBagConstraints.REMAINDER;
		
		// place the hex JTextField and JPanel in right JPanel
		jp_right.add(jtf_hex, gbc_jtf_hex);
		gbc_jtf_hex.ipadx = 90;
		gbc_jtf_hex.ipady = 90;
		gbc_jtf_hex.gridheight = GridBagConstraints.REMAINDER;
		jp_right.add(jp_color_display, gbc_jtf_hex);
		
		// place all components in a 
		GridBagConstraints gbc_main = new GridBagConstraints();
		gbc_main.ipady = 50;
		gbc_main.gridwidth = GridBagConstraints.RELATIVE;
		this.add(jp_left, gbc_main);
		gbc_main.gridwidth = GridBagConstraints.REMAINDER;
		this.add(jp_right, gbc_main);
		
		color_display = Color.black;
		update();
		
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider)e.getSource();
		
		// set corresponding value for each color : Red, Green, Blue
		if (source == js_red) {
			red_value = js_red.getValue();
			color_display = new Color(red_value, color_display.getGreen(), color_display.getBlue());
		}
		else if (source == js_green) {
			green_value = js_green.getValue();
			color_display = new Color(color_display.getRed(), green_value, color_display.getBlue());
		}
		else if (source == js_blue) {
			blue_value = js_blue.getValue();	
			color_display = new Color(color_display.getRed(), color_display.getGreen(), blue_value);
		}
		else {
			// do nothing
		}
		
		update();	
    }
	
	public void actionPerformed(ActionEvent e) {
		JTextField source = (JTextField)e.getSource();
		if(source == jtf_red) {
			red_value = Integer.parseInt(jtf_red.getText());
			color_display = new Color(red_value, color_display.getGreen(), color_display.getBlue());
		}
		else if (source == jtf_green) {
			green_value = Integer.parseInt(jtf_green.getText());
			color_display = new Color(color_display.getRed(), green_value, color_display.getBlue());
		}
		else if (source == jtf_blue) {
			blue_value = Integer.parseInt(jtf_blue.getText());
			color_display = new Color(color_display.getRed(), color_display.getGreen(), blue_value);
		}
		else if (source == jtf_hex) {
			hex_value = jtf_hex.getText();
			// length is limited to 6 because every color needs 2 digits
			if (hex_value.length() != 6){
				
			}
			else {
				color_display = Color.decode("0x" + hex_value);
			}	
		}
		else {
			// do nothing
		}
		
		update();
	}
	
	void update() {
		
		jtf_blue.setText(String.valueOf(color_display.getBlue()));
		jtf_red.setText(String.valueOf(color_display.getRed()));
		jtf_green.setText(String.valueOf(color_display.getGreen()));
		
		js_blue.setValue(color_display.getBlue());
		js_red.setValue(color_display.getRed());
		js_green.setValue(color_display.getGreen());
		
		String redS = Integer.toHexString(color_display.getRed());
		if (redS.length() == 1)
			redS = "0" + redS;
		
		String greenS = Integer.toHexString(color_display.getGreen());
		if (greenS.length() == 1)
			greenS = "0" + greenS;
		
		String blueS = Integer.toHexString(color_display.getBlue());
		if (blueS.length() == 1)
			blueS = "0" + blueS;
		
		jtf_hex.setText( redS + greenS + blueS);
		
		jp_color_display.setBackground(color_display);
	}

	public static void show_my_color_chooser() {
		MyColorChooser my_color_chooser = new MyColorChooser();
	}
	
	public static void main(String[] args) {
		MyColorChooser.show_my_color_chooser();
	}


}